//
//  NewfeatureViewController.swift
//  PersonnalDiary
//
//  Created by 徐宽阔 on 15/10/1.
//  Copyright © 2015年 XDKOO. All rights reserved.
//

import UIKit
import SnapKit

private let XDKNewfeatureReuseIdentifier = "XDKNewfeatureReuseIdentifier"

class NewfeatureViewController: UICollectionViewController {

    private let pageCount = 4
    private var layout = NewfeatureLayout()

    init() {
        // 注意: 在自定义构造方法的时候, 需要调用父类中指定设计构造函数
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("使用代码加载view")
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: XDKNewfeatureReuseIdentifier)
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
       return pageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(XDKNewfeatureReuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
        
        cell.imageIndex = indexPath.row
        return cell
    }
    
    
    // 完全显示一个cell之后调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        let cell = collectionView.cellForItemAtIndexPath(path) as! NewfeatureCell
        
        if path.item == (pageCount - 1)
        {
            cell.startAnimation()
        }
    }

}


class NewfeatureCell: UICollectionViewCell {
    
    var imageIndex: Int = 0{
        didSet{
            let image = UIImage(named: "new_feature_\(imageIndex + 1)")
            iconImageView.image = image
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    func startAnimation()
    {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    
    private func setupUI()
    {
        contentView.addSubview(iconImageView)
        contentView.addSubview(startButton)
        startButton.addTarget(self, action: Selector("startBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(0)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView.snp_bottom).offset(-160)
        }
    }
    
    func startBtnClick()
    {
        XDKLog("")
    }
    
    private lazy var iconImageView = UIImageView()
    
    private lazy var startButton: UIButton = {
        let btn = UIButton()
        btn.hidden = true
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        return btn
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("")
    }
}



class NewfeatureLayout: UICollectionViewFlowLayout {
    override func prepareLayout() {
        // 1.设置布局
        itemSize = UIScreen.mainScreen().bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        // 2.设置collectionView
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
    }
}


